package field

import "gitee.com/yanwc/net/utils"

type (
	Direct                 int
	DirectionSatellites    uint8
	DirectionSatellitesRaw [1]byte
)

const (
	North     Direct = iota // 正北
	EastNorth               // 东北
	East                    // 东
	EastSouth               // 东南
	South                   // 南
	WestSouth               // 西南
	West                    // 西
	WestNorth               // 西北
)

const (
	Sb8North     DirectionSatellites = 0b0000_0000 // North
	Sb8EastNorth DirectionSatellites = 0b0001_0000 // EastNorth
	Sb8East      DirectionSatellites = 0b0010_0000 // East
	Sb8EastSouth DirectionSatellites = 0b0011_0000 // EastSouth
	Sb8South     DirectionSatellites = 0b0100_0000 // South
	Sb8WestSouth DirectionSatellites = 0b0101_0000 // WestSouth
	Sb8West      DirectionSatellites = 0b0110_0000 // West
	Sb8WestNorth DirectionSatellites = 0b0111_0000 // WestNorth
)

const (
	Sb8Satellites DirectionSatellites = 0b0000_0111 // Bit3-bit0:卫星数（量化单位为3）Bit0为1时存在
)

const SatellitesUnit = 3

func NewDirect(ds DirectionSatellites) Direct {
	switch {
	case ds&Sb8North == Sb8North:
		return North
	case ds&Sb8EastNorth == Sb8EastNorth:
		return EastNorth
	case ds&Sb8East == Sb8East:
		return East
	case ds&Sb8EastSouth == Sb8EastNorth:
		return EastSouth
	case ds&Sb8South == Sb8South:
		return South
	case ds&Sb8WestNorth == Sb8WestNorth:
		return WestNorth
	case ds&Sb8West == Sb8West:
		return West
	case ds&Sb8WestSouth == Sb8WestSouth:
		return WestSouth
	}

	return 0
}

func (ds DirectionSatellites) Raw() (out DirectionSatellitesRaw) {
	copy(out[:], utils.NumberToBytesWithBigEndian(ds))
	return
}

func (ds DirectionSatellitesRaw) Type() DirectionSatellites {
	return utils.BytesToNumber[DirectionSatellites](ds[:])
}

func (ds DirectionSatellitesRaw) Slice() []byte {
	return ds[:]
}

func (ds DirectionSatellites) GetDirect() Direct {
	return NewDirect(ds)
}

func (ds DirectionSatellites) Satellites() int {
	return int(ds&Sb8Satellites) * SatellitesUnit
}
